package demo;

import okhttp3.*;

import java.io.IOException;

/**
 * @author lingkang
 * Created by 2024/5/10
 */
public class Demo09 {
    private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
    private final OkHttpClient client = new OkHttpClient();
    public void sendJson() throws IOException {
        // 创建你的JSON字符串
        String json = "{\"username\":\"lingkang\",\"password\":\"910405656\"}";

        // 将JSON字符串转换为RequestBody
        RequestBody requestBody = RequestBody.create(JSON, json);

        // 构建Request对象
        Request request = new Request.Builder()
                .url("https://lingkang.top/api/admin/login/precheck") // 替换为你的API端点
                .post(requestBody)
                .build();

        // 使用OkHttpClient发送请求，并接收响应
        Response response = client.newCall(request).execute();

        // 处理响应
        if (response.isSuccessful()) {
            String responseBody = response.body().string();
            // 处理响应体
            System.out.println(responseBody);
        }
    }

    public static void main(String[] args) {
        Demo09 example = new Demo09();
        try {
            example.sendJson();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
